<?php

namespace App\Http\Controllers\Seller;

use App\Http\Controllers\Controller;
use App\Http\Resources\Seller\AnnouncementResource\AnnouncementCollection;
use Illuminate\Http\Request;
use App\Models\Announcement;
use Illuminate\Http\Response;

class AnnouncementController extends Controller
{
    /**
     * Display a listing of the resource.
     *
     * @param Request $request
     * @param Announcement $announcement_model
     * @return array|Response
     */
    public function index(Request $request, Announcement $announcement_model)
    {

        if (!empty($request->name)) {
            $announcement_model = $announcement_model->where('name', 'like', '%' . $request->name . '%');
        }
        if (!empty($request->ename)) {
            $announcement_model = $announcement_model->where('ename', 'like', '%' . $request->ename . '%');
        }
        $list = $announcement_model->orderBy('id', 'desc')->paginate($request->per_page ?? 30);
        return $this->success(new AnnouncementCollection($list));
    }

    /**
     * Store a newly created resource in storage.
     *
     * @param Request $request
     * @param Announcement $announcement_model
     * @return array|Response
     */
    public function store(Request $request, Announcement $announcement_model)
    {
        $announcement_model->name    = $request->name ?? '';
        $announcement_model->ename   = $request->ename ?? '';
        $announcement_model->content = $request->contents ?? '';
        $announcement_model->save();
        return $this->success([], __('base.success'));
    }

    /**
     * Display the specified resource.
     *
     * @param Announcement $announcement_model
     * @param int $id
     * @return array|Response
     */
    public function show(Announcement $announcement_model, int $id)
    {
        $info = $announcement_model->find($id);
        return $this->success($info);
    }

    /**
     * Update the specified resource in storage.
     *
     * @param Request $request
     * @param Announcement $announcement_model
     * @param int $id
     * @return array|Response
     */
    public function update(Request $request, Announcement $announcement_model, int $id)
    {
        $announcement_model          = $announcement_model->find($id);
        $announcement_model->name    = $request->name;
        $announcement_model->ename   = $request->ename ?? '';
        $announcement_model->content = $request->contents?? '';
        $announcement_model->save();
        return $this->success([], __('base.success'));
    }

    /**
     * Remove the specified resource from storage.
     *
     * @param Announcement $announcement_model
     * @param int $id
     * @return array|Response
     */
    public function destroy(Announcement $announcement_model, int $id)
    {
        $idArray = array_filter(explode(',', $id), static function ($item) {
            return is_numeric($item);
        });
        $announcement_model->destroy($idArray);
        return $this->success([], __('base.success'));
    }
}
